package com.mosai.smartmedical;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.util.Arrays;

public class SensorActivity extends AppCompatActivity implements SensorEventListener {
    public static final String TAG = SensorActivity.class.getSimpleName();
    SensorManager sensorManager;
    Sensor sensor, accelerometer, magneticField;

    private float[] r = new float[9];   //rotation matrix
    private float[] values = new float[3];   //orientation values
    private float[] accelerometerValues;  //data of acclerometer sensor
    private float[] magneticFieldValues;  //data of magnetic field sensor


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: " + WebSettings.getDefaultUserAgent(this));
        setContentView(R.layout.activity_sensor);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);


//        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
//        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerSensorListener(accelerometer);
        registerSensorListener(magneticField);
    }

    private void registerSensorListener(Sensor sensor) {
        if (sensor != null) {
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                accelerometerValues = event.values;
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                magneticFieldValues = event.values;
                break;
        }

//        float[] rotationMatrix = new float[16];
//        SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
//        float[] remappedRotationMatrix = new float[16];
//        SensorManager.remapCoordinateSystem(rotationMatrix,
//                SensorManager.AXIS_X,
//                SensorManager.AXIS_Z,
//                remappedRotationMatrix);
//        float[] orientations = new float[3];
//        SensorManager.getOrientation(remappedRotationMatrix, orientations);
//        for (int i = 0; i < orientations.length; i++) {
//            orientations[i] = (float) (Math.toDegrees(orientations[i]));
//        }

        if (accelerometerValues == null || magneticFieldValues == null) {
            return;
        }
        if (!SensorManager.getRotationMatrix(r, null, accelerometerValues, magneticFieldValues)) {
            return;
        }
        SensorManager.getOrientation(r, values);

        for (int i = 0; i < values.length; i++) {
            values[i] = (float) (Math.toDegrees(values[i]));
        }

        Log.i(TAG, "onSensorChanged:"
                + " orientations: " + Arrays.toString(values)
        );
//        Log.i(TAG, "onSensorChanged:"
//                + " accuracy: " + event.accuracy
//                + " timestamp: " + event.timestamp
//                + " values: " + Arrays.toString(event.values)
//                + " orientations: " + Arrays.toString(values)
//        );
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.i(TAG, "onAccuracyChanged: accuracy: " + accuracy);
    }
}
